package com.miskatmobile.bb.linkedin;

public class StringUtil {
    /**
     * Returns the character (Unicode code point) at the specified index. 
     * The index refers to char values (Unicode code units) and ranges from 0 to length() - 1.
     * If the char value specified at the given index is in the high-surrogate range, the following index is less than the length of this String, 
     * and the char value at the following index is in the low-surrogate range, 
     * then the supplementary code point corresponding to this surrogate pair is returned. 
     * Otherwise, the char value at the given index is returned.
     * 
     * The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). 
     * Characters whose code points are greater than U+FFFF are called supplementary characters.
     * 
     * @param index the index to the char values 
     * @return the code point value of the character at the index
     */
    public static int codePointAt(String str, int index) {
            if (str.charAt(index) >= '\uD800' && str.charAt(index) <= '\uDBFF'
                    && str.charAt(index+1) >= '\uDC00' && str.charAt(index+1) < '\uDFFF' ) {
                    int highSurrogate = str.charAt(index) - 0xD800;
                    int lowSurrogate = str.charAt(index+1) - 0xDC00;
                    return highSurrogate * 0x400 + lowSurrogate + 0x10000;
            }
            return str.charAt(index);
    }
    
	/**
	 * �ж��ַ��ǲ���ȫ��
	 * 
	 * @param chs
	 * @return
	 */
	public static boolean isAllHalf(char chs) {
		return (!(('\uFF61' <= chs) && (chs <= '\uFF9F')) && !(('\u0020' <= chs) && (chs <= '\u007E')));
	}
	
	
	/**
	 * <p>
	 * Replace the string to given string
	 * </p>
	 * 
	 * @param src
	 *            the source
	 * @param what
	 *            replace which string
	 * @param repalceWhat
	 *            given string
	 * @param caseSensitive
	 *            case sensitive , if true ,will replace upper and lower char
	 * @return string or null
	 */
	public static String replaceAll(String src, String what,
			String repalceWhat, boolean caseSensitive) {
		if (src == null) {
			return null;
		}
		if (what == null || repalceWhat == null) {
			return src;
		}

		StringBuffer buffer = new StringBuffer(src);
		int postion = 0;
		int stringLength = what.length();
		String ls = what.toLowerCase();
		String us = what.toUpperCase();
		while (true) {
			// lower char or upper char
			if (caseSensitive) {
				postion = src.indexOf(ls, postion);
				if (-1 == postion) {
					postion = src.indexOf(us, 0);
				}
			} else {
				postion = src.indexOf(what, postion);
			}
			if (-1 == postion) {
				break;
			} else {
				buffer.delete(postion, postion + stringLength);
				buffer.insert(postion, repalceWhat);
				src = buffer.toString();
			}
		}
		return src;
	}
}
